package com.fuyao.cloud.admin.flow.actor;

import com.fuyao.cloud.flowable.entity.ActorUser;
import com.fuyao.cloud.admin.api.entity.SysUser;
import com.fuyao.cloud.flowable.vo.ActorInstance;
import com.fuyao.cloud.admin.service.SysUserService;
import com.fuyao.cloud.common.security.service.CloudUser;
import org.apache.commons.compress.utils.Lists;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


/**
 * 文档作者
 */
@Component("docAuthorRule")
public class DocAuthorRule implements ActorRule {

    @Override
    public String getName() {
        return "文档作者";
    }

    @Override
    public String getCode() {
        return "AR_DOC_AUTHOR";
    }

    @Override
    public int getArgNum() {
        return 0;
    }

    @Override
    public String[] getArgsDesc() {
        return new String[0];
    }

    @Autowired
    private SysUserService userService;

    @Resource
    private RuntimeService runtimeService;

    @Override
    public List<ActorInstance> execute(CloudUser cloudUser, ActorUser actorUser, String procInstId) {

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(procInstId).singleResult();
        String startUserId = processInstance.getStartUserId();
        List<ActorInstance> actorInstances = Lists.newArrayList();
        SysUser sysUser = this.userService.getUserByUsername(startUserId);
        ActorInstance actorInstance = new ActorInstance(sysUser);
        actorInstances.add(actorInstance);
        return actorInstances;
    }
}
